using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using QianJin.Models;
using QianJin.Views;
using QianJin.Views.Shopping;

namespace QianJin.Controllers
{
    public class ShoppingController : BaseController
    {
        public ActionResult AddShoppingItem(string productId, string returnUrl)
        {
            this.CurrentUser.ShoppingCart.AddProduct(productId);
            return Redirect(returnUrl);
        }

        public ActionResult RemoveShoppingItem(string productId, string returnUrl)
        {
            this.CurrentUser.ShoppingCart.RemoveProduct(productId);
            return Redirect(returnUrl);
        }

        public ActionResult ListShoppingItem()
        {
            return View("ShoppingList", new ShoppingListData() { User = this.CurrentUser });
        }

        public ActionResult ShowFillUserInfo()
        {
            return View("FillUserInfo", new UserInfoData() { User = this.CurrentUser }); ;
        }

        public ActionResult FillInUserInfo(UserInfo user, string returnUrl)
        {
            this.CurrentUser.Name = user.Name;
            this.CurrentUser.Email = user.Email;
            this.CurrentUser.Address = user.Address;
            this.CurrentUser.Phone = user.Phone;
            this.CurrentUser.Comment = user.Comment;
            this.CurrentUser.Company = user.Company;

            try
            {
                this.CurrentUser.SendOrderEmail();
                this.CurrentUser.ShoppingCart.ShoppingItems.Clear();
                return View("SuccessOrdered", new SuccessOrderedData() { User = this.CurrentUser });
            }
            catch (Exception e)
            {
                return View("FailOrdered", new FailOrderedData() { User = this.CurrentUser, Exp = e });
            }
        }

        public ActionResult ShowUserConfirmOrder()
        {
            return View("UserConfirm", new UserConfirmViewData() { User = this.CurrentUser });

        }

        public ActionResult SendOrderEmail()
        {
            try
            {
                this.CurrentUser.SendOrderEmail();
                this.CurrentUser.ShoppingCart.ShoppingItems.Clear();
                return View("SuccessOrdered", new SuccessOrderedData() { User = this.CurrentUser });
            }
            catch (Exception e)
            {
                return View("FailOrdered", new FailOrderedData() { User = this.CurrentUser, Exp = e});
            }
        }
    }
}
